package com.bloom.demo.utils;

import redis.clients.jedis.util.MurmurHash;

/**
 * Offer the offset of redis setbit using diff hash algorithm:
 */
public class HashUtils {

    /**
     * get the setbit offset by MurmurHash
     * 高效
     * 分布均匀
     * 非加密，不安全适用于不需要安全性的场景
     *
     * @param bizId
     * @return
     */
    public static int[] murmurHashOffset(String bizId, int hashFunctionCount, int maxBitCount) {
        int[] offsets = new int[hashFunctionCount];
        byte[] b = bizId.getBytes();
        int hash1 = MurmurHash.hash(b, 0);
        int hash2 = MurmurHash.hash(b, hash1);
        for (int i = 0; i < hashFunctionCount; ++i) {
            offsets[i] = Math.abs((hash1 + i * hash2) % maxBitCount);
        }
        return offsets;
    }

    public static void main(String[] args) {
        int[] offsets = murmurHashOffset("528804111363644L", 6, 1000);
        System.out.println(offsets);
    }
}
